package algorithm.test;

import algorithm.SortUtils;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 开启四个线程创建随机数组
 *随机数组的排序时间比较
 * 快排、堆排、归并
 */
public class Main implements Runnable{
    public static CountDownLatch countDownLatch = new CountDownLatch(4);//
    private Random random;
    private static final int SIZE = 40000000;//数组长度
    public static int[] num = new int[SIZE];
    private int start;//每个线程负责的下标起始位置
    private int len;//每个线程的结束位置
    public Main(int i, int j){
        start = i;
        len = j;
        random = new Random();
    }
    @Override
    public void run() {
        for(int i=0;i<len;i++){
            num[i+start] = random.nextInt(SIZE);
        }
        countDownLatch.countDown();//线程任务结束
    }
    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<4;i++){
            new Thread(new Main(i*10000000,10000000)).start();
        }
        countDownLatch.await();//等待所有线程创建任务完成
        int[] clone = num.clone();
        int[] clone1 = num.clone();
        long t = System.currentTimeMillis();
        SortUtils.quickSort(clone);
        System.out.println("quickSort:"+(System.currentTimeMillis()-t)+"ms");
        t = System.currentTimeMillis();
        SortUtils.quickSort(clone1);
        System.out.println("mergeSort:"+(System.currentTimeMillis()-t)+"ms");
        t = System.currentTimeMillis();
        SortUtils.heapSort(num);
        System.out.println("heapSort:"+(System.currentTimeMillis()-t)+"ms");

    }
}